<template>
  <div>
    <zh-card title="">
      <zh-form :model="pageData.queryParams" ref="queryFormRef">
        <zh-form-input prop="interfaceId" label="动态接口id" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.interfaceId"></zh-form-input>
        <zh-form-input prop="funcCode" label="功能编号" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.funcCode"></zh-form-input>
        <zh-form-input prop="tableId" label="逻辑表格id" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.tableId"></zh-form-input>
        <zh-form-select prop="funcType" label="功能类型" allowClear :span="8" :options="pageData.dictData.FUNCTYPE"
                        v-model="pageData.queryParams.funcType"></zh-form-select>
        <zh-form-query @queryClick="queryLogicTableDynamicInterface" :formRef="queryFormRef" :show-advanced="false">
          <el-button @click="showAddDialog">添加</el-button>
        </zh-form-query>
      </zh-form>
    </zh-card>
    <zh-table title="逻辑表动态接口列表" ref="logicTableDynamicInterfaceTable"
              size="mini"
              :funcCode="'logicTableDynamicInterface0001'"
              :columns="pageData.tableColumns"
              highlight-current-row>
      <template #op="scope">
        <el-tooltip content="编辑" placement="top">
          <el-button :icon="Edit" link @click="showEditDialog(scope)"></el-button>
        </el-tooltip>
        <el-tooltip content="删除" placement="top">
          <el-button :icon="Delete" type="danger" link @click="delLogicTableDynamicInterface(scope.row)"></el-button>
        </el-tooltip>
        <el-tooltip content="详情" placement="top">
          <el-button :icon="Document" link @click="showDetail(scope)"></el-button>
        </el-tooltip>
        <el-tooltip content="执行" placement="top">
          <el-button :icon="VideoPlay" link @click="callInterface(scope.row)"></el-button>
        </el-tooltip>
      </template>
    </zh-table>
    <el-dialog title="添加逻辑表动态接口" v-model="pageData.addLogicTableDynamicInterfaceDialogVisible">
      <zh-form :rules="pageData.formRules">
        <zh-form-input prop="interfaceId" label="动态接口id" type="text" :span="8" dataType="common"
                       v-model="pageData.formData.interfaceId"></zh-form-input>
        <zh-form-input prop="funcCode" label="功能编号" type="text" :span="8" dataType="common"
                       v-model="pageData.formData.funcCode"></zh-form-input>
        <zh-form-input prop="tableId" label="逻辑表格id" type="text" :span="8" dataType="common"
                       v-model="pageData.formData.tableId"></zh-form-input>
        <zh-form-select prop="funcType" label="功能类型" allowClear :span="8" :options="pageData.dictData.FUNCTYPE"
                        v-model="pageData.formData.funcType"></zh-form-select>
      </zh-form>
      <template #footer>
        <span class="dialog-footer">

          <el-button @click="pageData.addLogicTableDynamicInterfaceDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addLogicTableDynamicInterface">
            保存
          </el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog title="修改逻辑表动态接口" v-model="pageData.editLogicTableDynamicInterfaceDialogVisible">
      <zh-form :rules="pageData.formRules">
        <zh-form-input prop="interfaceId" label="动态接口id"
                       v-model="pageData.editFormData.interfaceId"></zh-form-input>
        <zh-form-input prop="funcCode" label="功能编号"
                       v-model="pageData.editFormData.funcCode"></zh-form-input>
        <zh-form-input prop="tableId" label="逻辑表格id"
                       v-model="pageData.editFormData.tableId"></zh-form-input>
        <zh-form-select prop="funcType" label="功能类型" allowClear :options="pageData.dictData.FUNCTYPE"
                        v-model="pageData.editFormData.funcType"></zh-form-select>
      </zh-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="pageData.editLogicTableDynamicInterfaceDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="editLogicTableDynamicInterface">
            保存
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup name="logicTableDynamicInterface">
import {Delete, Edit, Document, VideoPlay} from '@element-plus/icons-vue'
import {ElMessageBox} from "element-plus";
// ------------------接收传入参数(组件)-------------------

// ------------------钩子函数----------------------------
// 初始化
onMounted(() => {
  init();
})

// ------------------定义子组件--------------------------
const logicTableDynamicInterfaceTable = ref();
const queryFormRef = ref();
// ------------------变量区------------------------------
const pageData = reactive({
  addLogicTableDynamicInterfaceDialogVisible: false,
  editLogicTableDynamicInterfaceDialogVisible: false,
  detailDialog: false,
  tableColumns: [
    {type: 'seq', width: 60, fixed: 'left', align: 'center', title: '序号'},
    {field: 'interfaceId', title: '动态接口id', visible: true, align: 'center', width: 100},
    {field: 'funcCode', title: '功能编号', visible: true, align: 'center', width: 100},
    {field: 'tableId', title: '逻辑表格id', visible: true, align: 'center', width: 100},
    {field: 'funcType', title: '功能类型', visible: true, align: 'center', width: 100},
    {field: 'op', title: '操作', visible: true, align: 'center', width: 100, fixed: 'right', slots: {default: 'op'}},
  ],
  queryParams: {
    interfaceId: '', funcCode: '', tableId: '', funcType: ''
  },
  formData: {
    interfaceId: '', funcCode: '', tableId: '', funcType: ''
  },
  editFormData: {
    interfaceId: '', funcCode: '', tableId: '', funcType: ''
  },
  formRules: {},
  dictFields: 'FUNCTYPE',
  dictData: [],
});
// ------------------方法区------------------------------
const init = () => {
  getDict();
  queryLogicTableDynamicInterface();
}
// 查询数据字典信息
const getDict = () => {
  let params = {
    dictFields: pageData.dictFields,
  };
  $api.common.getBatchDict.post(params).then((res: any) => {
    pageData.dictData = res.result;
  });
};
const queryLogicTableDynamicInterface = () => {
  logicTableDynamicInterfaceTable.value.query(pageData.queryParams);
}
const showAddDialog = () => {
  pageData.addLogicTableDynamicInterfaceDialogVisible = true;
  pageData.formData = {
    interfaceId: '', funcCode: '', tableId: '', funcType: ''
  }
}
const showEditDialog = (scope) => {
  pageData.editLogicTableDynamicInterfaceDialogVisible = true;
  pageData.editFormData = {...scope.row}
}
const addLogicTableDynamicInterface = () => {
  $api.common.send.post('logicTableDynamicInterface0004', pageData.formData).then(res => {
    if (res.code == 0) {
      ElMessageBox.alert('添加逻辑表动态接口成功');
      pageData.addLogicTableDynamicInterfaceDialogVisible = false;
      queryLogicTableDynamicInterface();
    }
  })
}
const editLogicTableDynamicInterface = () => {
  $api.common.send.post('logicTableDynamicInterface0005', pageData.editFormData).then(res => {
    if (res.code == 0) {
      ElMessageBox.alert('修改逻辑表动态接口成功');
      pageData.editLogicTableDynamicInterfaceDialogVisible = false;
      queryLogicTableDynamicInterface();
    }
  })
}
const delLogicTableDynamicInterface = ({interfaceId}) => {
  ElMessageBox.confirm('确定删除该逻辑表动态接口?').then(() => {
    $api.common.send.post('logicTableDynamicInterface0006', {interfaceId: interfaceId}).then(res => {
      if (res.code == 0) {
        ElMessageBox.alert('删除逻辑表动态接口成功');
        queryLogicTableDynamicInterface();
      }
    })
  })
}

const callInterface = (row) => {
  $api.common.send.post(row.funcCode, {...row, params: {dictField: 'USERTYPE', dictKey: '', value: '', sysId: ''}}).then(res => {
    if (res.code == 0) {
      ElMessageBox.alert('调用接口成功');
    }
  })
}

</script>
<style lang="scss" scoped></style>
